<template>
    <div class="rescueGoods"
         style="height:100%;">
        <div class="form-item form-item-style1">
            <div class="label-title">
                <label>单位名称</label>
            </div>
            <div class="equipment-right-select"
                 @click="companyPropShow = true"
                 v-if="userInfo.departsir != '1'">
                <span>{{checkcompany.Name?checkcompany.Name:'请选择'}}</span>
                <i class="iconfont icon-jinru rightmsg"></i>
            </div>
            <div class="equipment-right-select"
                 v-else>
                <span>{{checkcompany.Name}}</span>
            </div>
        </div>
        <div class="form-item form-item-style1">
            <div class="label-title">
                <label>物资类型</label>
            </div>
            <group>
                <popup-picker :data="goodsType"
                              placeholder='默认全部'
                              v-model="goodsTypeVal"
                              @on-change="onPullingDown"></popup-picker>
            </group>
        </div>
        <div class="form-item form-item-style1">
            <div class="label-title">
                <label>开始时间</label>
            </div>
            <group>
                <datetime v-model="startTime"
                          @on-change="onPullingDown"></datetime>
            </group>
        </div>
        <div class="form-item form-item-style1">
            <div class="label-title">
                <label>结束时间</label>
            </div>
            <group>
                <datetime v-model="endTime"
                          @on-change="onPullingDown"></datetime>
            </group>
        </div>
        <h3>总数：{{listAllCount}}</h3>
        <noData v-if="lists.length == 0"></noData>
        <div class="betterScroll">
            <vue-better-scroll ref="scroll"
                               :scrollbar="scrollbarObj"
                               :pullDownRefresh="pullDownRefreshObj"
                               :pullUpLoad="pullUpLoadObj"
                               :startY="parseInt(startY)"
                               @pullingDown="onPullingDown"
                               @pullingUp="onPullingUp">
                <div class="lists">
                    <ul>
                        <li v-for="(item,index) in lists"
                            :key="index">
                            <router-link :to="{name:'goodsDetail',params:{
                                detail:item
                            }}">
                                <div>
                                    <h5>{{item.GoodsName}}</h5>
                                    <div class="flex">
                                        <p>{{item.ProjectName}}</p>
                                        <div class="status">
                                            <span class="orange"
                                                  v-if="item.IsValidity">【即将过期】</span>
                                            <x-icon type="ios-arrow-right"
                                                    size="20"
                                                    class="icon-right"></x-icon>
                                        </div>
                                    </div>
                                    <p>
                                        <span>入库时间：{{item.GoodsTime?item.GoodsTime.split(' ')[0]:''}}</span>
                                        <span>有效期：{{item.GoodsValidity?item.GoodsValidity.split(' ')[0]:''}}</span>
                                    </p>
                                </div>
                            </router-link>
                        </li>
                    </ul>
                </div>
            </vue-better-scroll>
            <myMenu></myMenu>
            <activityCompany :selectCompany="selectCompany"
                             :companyPropShow="companyPropShow"></activityCompany>
        </div>
    </div>
</template>
<script>
import formatDate from '@/utils/formatDate'
import VueBetterScroll from 'vue2-better-scroll'
import myMenu from '@/commonComponents/menu'
import activityCompany from '@/commonComponents/activityCompany'
export default {
    name: 'rescueGoods',
    data () {
        return {
            // dropDown: false
            // 这个配置可以开启滚动条，默认为 false。当设置为 true 或者是一个 Object 的时候，都会开启滚动条，默认是会 fade 的
            scrollbarObj: {
                fade: true
            },
            // 这个配置用于做下拉刷新功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启下拉刷新，可以配置顶部下拉的距离（threshold） 来决定刷新时机以及回弹停留的距离（stop）
            pullDownRefreshObj: {
                threshold: 90,
                stop: 40
            },
            // 这个配置用于做上拉加载功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启上拉加载，可以配置离底部距离阈值（threshold）来决定开始加载的时机
            pullUpLoadObj: {
                threshold: 0,
                txt: {
                    more: '加载更多',
                    noMore: '没有更多数据了'
                }
            },
            startY: 0, // 纵轴方向初始化位置
            scrollToX: 0,
            scrollToY: 0,
            scrollToTime: 700,
            startTime: `${formatDate().split('-')[0]}-${formatDate().split('-')[1]}-01`, // 开始时间
            endTime: formatDate(), // 结束时间
            checkcompany: {
            }, //  单位名称
            userInfo: {},
            goodsTypeData: '',
            goodsType: [[]], //  设备类型
            goodsTypeVal: ['全部'], // 选中设备类型
            ownEquipment: [['全部', '是', '否']], // 是否自有设备
            ownEquipmentVal: ['全部'],
            lists: [],
            pageIndex: 0, // 当前页
            listAllCount: 0, // 总条数
            companyPropShow: false
        }
    },
    components: {
        VueBetterScroll,
        myMenu,
        activityCompany
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
        this.checkcompany = {
            Name: this.userInfo.deptname,
            Id: this.userInfo.deptid
        }
        this.getGoodsType()
        this.onPullingDown()
    },
    methods: {
        getGoodsType () { // 获取设备类型
            let params = {
                'allowPaging': true,
                'business': 'GetGoodsType',
                'tokenId': '',
                'userId': this.userInfo.userid
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    this.goodsTypeData = res.data.data
                    this.goodsType[0].push('全部')
                    this.goodsTypeData.forEach((item) => {
                        this.goodsType[0].push(item.Content)
                    })
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        },
        selectType () {
            this.companyPropShow = true
        },
        handleSubmit () {
            this.$vux.loading.show({
                text: '加载中...'
            })
            let params = {
                'allowPaging': true,
                'business': 'GetEmrtGoodsList',
                'data': {
                    'DepartId': this.checkcompany.Id, // 单位id
                    'EndTime': this.endTime, // 结束时间
                    'GoodsType': this.goodsTypeVal[0] == '全部' ? '' : this.goodsTypeVal[0], // 物资类型
                    'StartTime': this.startTime// 开始时间
                },
                'pageIndex': this.pageIndex,
                'pageSize': 20,
                'tokenId': '',
                'userId': this.userInfo.userid
            }
            for (let i = 0; i < this.goodsTypeData.length; i++) {
                if (this.goodsTypeVal[0] == this.goodsTypeData[i].Content) {
                    params.data.GoodsType = this.goodsTypeData[i].CodeName
                    break
                }
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    this.listAllCount = res.data.count
                    this.lists = this.lists.concat(res.data.data)
                    if (this.lists.length < this.listAllCount) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        },
        onPullingDown () {
            // 模拟下拉刷新
            this.listCount = 0
            this.pageIndex = 0
            this.lists = []
            this.handleSubmit()
        },
        onPullingUp () {
            this.pageIndex++
            this.handleSubmit()
        },
        selectCompany (item) {
            this.companyPropShow = false
            this.checkcompany = {
                Name: item.deptName,
                Id: item.deptId
            }
        }
    }
}
</script>
<style scoped>
h3 {
    height: 40px;
    line-height: 40px;
    color: #999;
    font-size: 14px;
    background: #ebeef2;
    padding-right: 20px;
    text-align: right;
}
.betterScroll {
    position: absolute;
    top: 260px;
    left: 0;
    bottom: 0;
    right: 0;
    overflow: hidden;
}
</style>
<style lang="less">
.rescueGoods {
    .lists {
        padding-left: 20px;
        a {
            display: block;
            // display: flex;
            // justify-content: space-between;
            // align-items: center;
            padding: 14px 20px 14px 0;
            border-bottom: 1px solid #efeff4;
        }
        h5 {
            line-height: 18px;
            color: #333;
            font-size: 14px;
        }
        p,
        span {
            font-size: 14px;
            color: #999;
            line-height: 18px;
        }
        .flex {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .status {
            display: flex;
            align-items: center;
            span {
                font-size: 12px;
            }
            .orange {
                color: #fe7234;
            }
        }
    }
}
</style>
